//
//  LLBToastManager.swift
//  DZNEmptyDataSet
//
//  Created by Yun on 2018/12/18.
//

import UIKit
import LLBUtil
import Toast_Swift

/// 显示toast
///
/// - Parameters:
///   - title: 标题
///   - message: 内容
///   - img: 图片
///   - duration: 时间，默认3秒
///   - position: 位置，默认中间
func showToastMessage(title:String, message: String, duration:TimeInterval = 2,position:ToastPosition = .center){
    DispatchQueue.main.safeAsync {
        if let vc = getCurrentController(){
            var style = ToastStyle()
            style.backgroundColor = UIColor.hex(from: 0xFF666666)
            style.titleFont = llbMediumFont(size: 17)
            style.messageFont = llbFont(size: 14)
            style.titleAlignment = .center
            style.messageAlignment = .center
            style.cornerRadius = 4
            ToastManager.shared.isTapToDismissEnabled = true
            vc.view.makeToast(message, duration: duration,position: position, title: title, style: style, completion: { (finish) in
            })
        }
    }
}



public func showErrorMessage(_ message: String, position:ToastPosition = .center){
    showToastMessage(title: "错误", message: message, position: position)
}

public func showSucessMessage(_ message: String, position:ToastPosition = .center){
    showToastMessage(title: "成功", message: message, position: position)
}

public func showNoticeMessage(_ message: String, position:ToastPosition = .center){
    showToastMessage(title: "提醒", message: message, position: position)
}

public func showWarningMessage(_ message: String, position:ToastPosition = .center){
    showToastMessage(title: "警告", message: message, position: position)
}


public struct SCLButtonAction{
    var title:String = ""
    var style:UIAlertAction.Style = .default
    var action:(()->Void)?
    
    public init(title:String = "",style:UIAlertAction.Style = .default,action:(()->Void)?) {
        self.title = title
        self.style = style
        self.action = action
    }
}

public func showAlertMessageAndButtons(_ title:String = "提醒",_ message:String = "",preferStyle:UIAlertController.Style = .alert,_ buttons:SCLButtonAction ...){
    
    let alert = UIAlertController(title: title, message: message, preferredStyle: preferStyle)
    for button in buttons{
        alert.addAction(UIAlertAction(title: button.title, style: button.style, handler: { (alert) in
            if(button.action != nil){
                button.action!()
            }
        }))
    }
    let vc = getCurrentController()
    vc?.present(alert, animated: true, completion: nil)
}

